package com.xinlan.imageeditlibrary.editimage;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xinlan.imageeditlibrary.BaseActivity;
import com.xinlan.imageeditlibrary.CallbackEventBiz;
import com.xinlan.imageeditlibrary.Configs;
import com.xinlan.imageeditlibrary.R;
import com.xinlan.imageeditlibrary.editimage.fliter.PhotoProcessing;
import com.xinlan.imageeditlibrary.editimage.fragment.AddTextFragment;
import com.xinlan.imageeditlibrary.editimage.fragment.CropFragment;
import com.xinlan.imageeditlibrary.editimage.fragment.FliterListFragment;
import com.xinlan.imageeditlibrary.editimage.fragment.MainMenuFragment;
import com.xinlan.imageeditlibrary.editimage.fragment.RotateFragment;
import com.xinlan.imageeditlibrary.editimage.fragment.StirckerFragment;
import com.xinlan.imageeditlibrary.editimage.utils.BitmapUtil;
import com.xinlan.imageeditlibrary.editimage.utils.BitmapUtils;
import com.xinlan.imageeditlibrary.editimage.view.CropImageView;
import com.xinlan.imageeditlibrary.editimage.view.CustomViewPager;
import com.xinlan.imageeditlibrary.editimage.view.RotateImageView;
import com.xinlan.imageeditlibrary.editimage.view.StickerView;
import com.xinlan.imageeditlibrary.editimage.view.TextStickerView;
import com.xinlan.imageeditlibrary.editimage.view.imagezoom.ImageViewTouch;
import com.xinlan.imageeditlibrary.editimage.view.imagezoom.ImageViewTouchBase;

/**
 * 图片编辑 主页面
 * 
 * @author ypy
 *         <p>
 *         包含 1.贴图 2.滤镜 3.剪裁 4.底图旋转 功能
 */
public class EditImageActivity extends BaseActivity {
	public static final String FILE_PATH = "file_path";
	public static final String EXTRA_OUTPUT = "extra_output";

	public static final int MODE_NONE = 0;
	// public static final int MODE_STICKERS = 1;// 贴图模式
	// public static final int MODE_FILTER = 2;// 滤镜模式
	public static final int MODE_CROP = 3;// 剪裁模式
	public static final int MODE_ROTATE = 4;// 旋转模式
	public static final int MODE_TEXT = 5;// 文字模式

	public String filePath;// 需要编辑图片路径
	public String saveFilePath;// 生成的新图片路径
	private int imageWidth, imageHeight;// 展示图片控件 宽 高
	private LoadImageTask mLoadImageTask;

	public int mode = MODE_NONE;// 当前操作模式
	private EditImageActivity mContext;
	public Bitmap mainBitmap;// 底层显示Bitmap
	public ImageViewTouch mainImage;
	private View backBtn;

	public ViewFlipper bannerFlipper;
	private View applyBtn;// 应用按钮
	private View saveBtn;// 保存按钮

	public TextView image_size;// 尺寸信息显示

	public StickerView mStickerView;// 贴图层View
	public CropImageView mCropPanel;// 剪切操作控件
	public RotateImageView mRotatePanel;// 旋转操作控件
	public TextStickerView mTextStickerView;// 文本贴图显示View

	public CustomViewPager bottomGallery;// 底部gallery
	private BottomGalleryAdapter mBottomGalleryAdapter;// 底部gallery
	private MainMenuFragment mMainMenuFragment;// Menu
	public StirckerFragment mStirckerFragment;// 贴图Fragment
	public FliterListFragment mFliterListFragment;// 滤镜FliterListFragment
	private CropFragment mCropFragment;// 图片剪裁Fragment
	public RotateFragment mRotateFragment;// 图片旋转Fragment
	public AddTextFragment mAddTextFragment;

	private Bitmap fliterBit;// 滤镜处理后的bitmap

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		checkInitImageLoader();
		setContentView(R.layout.activity_image_edit);
		initView();
		getData();
	}

	private void getData() {
		filePath = getIntent().getStringExtra(FILE_PATH);
		saveFilePath = getIntent().getStringExtra(EXTRA_OUTPUT);// 保存图片路径
		loadImage(filePath);
	}

	public void updateImageSize() {
		image_size.setText("宽: " + mainBitmap.getWidth() * 50 / 1024 + "  "
				+ " 高: " + mainBitmap.getHeight() * 50 / 1024 + "   (毫米)"
				+ "\n点击更改");
	}

	private void showAlertDialog() {
		ConfirmDialog ConfirmDialog = new ConfirmDialog(EditImageActivity.this,
				true);
		ConfirmDialog.show();
	}

	public class ConfirmDialog extends Dialog {

		private EditText confirm_dialog_et;
		private Button mBtnConfirm;
		private Button mBtnCancel;

		/**
		 * @param context
		 *            资源上下文
		 * @param cancelable
		 *            是否可以取消
		 */
		public ConfirmDialog(Context context, boolean cancelable) {
			super(context, R.style.CommonDialogStyle);
			View contentView = View.inflate(context, R.layout.confirm_dialog,
					null);
			setContentView(contentView);
			setCancelable(cancelable);
			confirm_dialog_et = (EditText) contentView
					.findViewById(R.id.confirm_dialog_et);
			mBtnConfirm = (Button) contentView
					.findViewById(R.id.confirm_dialog_btn_confirm);
			mBtnCancel = (Button) contentView
					.findViewById(R.id.confirm_dialog_btn_cancel);
			mBtnConfirm.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (TextUtils.isEmpty(confirm_dialog_et.getText())) {
						Toast.makeText(mContext, "数值不能为空", Toast.LENGTH_SHORT)
								.show();
						return;
					}
					if (Integer.valueOf(confirm_dialog_et.getText().toString()) > 50) {
						Toast.makeText(mContext, "数值不能大于50", Toast.LENGTH_SHORT)
								.show();
						return;
					}
					mainBitmap = BitmapUtil.getFittingScreenBitmap(mainBitmap,
							(Integer.valueOf(confirm_dialog_et.getText()
									.toString())) * 1024 / 50 + 1, (Integer
									.valueOf(confirm_dialog_et.getText()
											.toString())) * 1024 / 50 + 1,
							false, true);
					mainImage.setImageBitmap(mainBitmap);
					mainImage
							.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
					updateImageSize();

					File f = new File(saveFilePath);
					if (f.exists()) {
						f.delete();
					}
					try {
						FileOutputStream out = new FileOutputStream(f);
						mainBitmap
								.compress(Bitmap.CompressFormat.PNG, 100, out);
						out.flush();
						out.close();
					} catch (FileNotFoundException e) {
					} catch (Exception e) {
					}
					dismiss();
				}
			});
			mBtnCancel.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					dismiss();
				}
			});
		}
	}

	private void initView() {
		mContext = this;
		DisplayMetrics metrics = getResources().getDisplayMetrics();
		imageWidth = metrics.widthPixels / 2;
		imageHeight = metrics.heightPixels / 2;

		bannerFlipper = (ViewFlipper) findViewById(R.id.banner_flipper);
		bannerFlipper.setInAnimation(this, R.anim.in_bottom_to_top);
		bannerFlipper.setOutAnimation(this, R.anim.out_bottom_to_top);
		applyBtn = findViewById(R.id.apply);
		applyBtn.setOnClickListener(new ApplyBtnClick());
		saveBtn = findViewById(R.id.save_btn);
		saveBtn.setOnClickListener(new SaveBtnClick());

		image_size = (TextView) findViewById(R.id.image_size);
		image_size.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showAlertDialog();
			}
		});

		mainImage = (ImageViewTouch) findViewById(R.id.main_image);
		backBtn = findViewById(R.id.back_btn);// 退出按钮
		backBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				forceReturnBack();
			}
		});

		mStickerView = (StickerView) findViewById(R.id.sticker_panel);
		mCropPanel = (CropImageView) findViewById(R.id.crop_panel);
		mRotatePanel = (RotateImageView) findViewById(R.id.rotate_panel);
		mTextStickerView = (TextStickerView) findViewById(R.id.text_sticker_panel);

		// 底部gallery
		bottomGallery = (CustomViewPager) findViewById(R.id.bottom_gallery);
		// bottomGallery.setOffscreenPageLimit(5);
		mMainMenuFragment = MainMenuFragment.newInstance(this);
		mBottomGalleryAdapter = new BottomGalleryAdapter(
				this.getSupportFragmentManager());
		mStirckerFragment = StirckerFragment.newInstance(this);
		mFliterListFragment = FliterListFragment.newInstance(this);
		mCropFragment = CropFragment.newInstance(this);
		mRotateFragment = RotateFragment.newInstance(this);
		mAddTextFragment = AddTextFragment.newInstance(this);
		bottomGallery.setAdapter(mBottomGalleryAdapter);

		mainImage.setFlingListener(new ImageViewTouch.OnImageFlingListener() {
			@Override
			public void onFling(MotionEvent e1, MotionEvent e2,
					float velocityX, float velocityY) {
				if (velocityY > 1) {
					closeInputMethod();
				}
			}
		});
	}

	/**
	 * 关闭输入法
	 */
	private void closeInputMethod() {
		if (mAddTextFragment.isAdded()) {
			mAddTextFragment.hideInput();
		}
	}

	/**
	 * @author panyi
	 */
	private final class BottomGalleryAdapter extends FragmentPagerAdapter {
		public BottomGalleryAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int index) {
			// System.out.println("createFragment-->"+index);
			switch (index) {
			case MainMenuFragment.INDEX:// 主菜单
				return mMainMenuFragment;
			case StirckerFragment.INDEX:// 贴图
				return mStirckerFragment;
			case FliterListFragment.INDEX:// 滤镜
				return mFliterListFragment;
			case CropFragment.INDEX:// 剪裁
				return mCropFragment;
			case RotateFragment.INDEX:// 旋转
				return mRotateFragment;
			case AddTextFragment.INDEX:// 添加文字
				return mAddTextFragment;
			}// end switch
			return MainMenuFragment.newInstance(mContext);
		}

		@Override
		public int getCount() {
			return 6;
		}
	}// end inner class

	/**
	 * 异步载入编辑图片
	 * 
	 * @param filepath
	 */
	public void loadImage(String filepath) {
		if (mLoadImageTask != null) {
			mLoadImageTask.cancel(true);
		}
		mLoadImageTask = new LoadImageTask();
		mLoadImageTask.execute(filepath);
	}

	private final class LoadImageTask extends AsyncTask<String, Void, Bitmap> {
		@Override
		protected Bitmap doInBackground(String... params) {
			if (Configs.isText) {
				return ImageLoader.getInstance().loadImageSync(
						"assets://black_bg.png");
			}
			return BitmapUtils.getSampledBitmap(params[0], imageWidth,
					imageHeight);
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			super.onPostExecute(result);
			if (mainBitmap != null) {
				mainBitmap.recycle();
				mainBitmap = null;
				System.gc();
			}
			if (result.getWidth() > 1024 || result.getHeight() > 1024) {
				result = BitmapUtil.getFittingScreenBitmap(result, 1024, 1024,
						false, true);
				// mainImage.setImageBitmap(mainBitmap);
				// mainImage
				// .setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
				// updateImageSize();
				File f = new File(saveFilePath);
				if (f.exists()) {
					f.delete();
				}
				try {
					FileOutputStream out = new FileOutputStream(f);
					result.compress(Bitmap.CompressFormat.PNG, 100, out);
					out.flush();
					out.close();
				} catch (FileNotFoundException e) {
				} catch (Exception e) {
				}
			}
			mainBitmap = result;
			mainImage.setImageBitmap(result);
			mainImage
					.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
			// mainImage.setDisplayType(DisplayType.FIT_TO_SCREEN);
			mFliterListFragment.aotuHb();
			updateImageSize();
		}
	}// end inner class

	/**
	 * 按下返回键
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			switch (mode) {
			// case MODE_STICKERS:
			// mStirckerFragment.backToMain();
			// return true;
			// case MODE_FILTER:// 滤镜编辑状态
			// mFliterListFragment.backToMain();// 保存滤镜贴图
			// return true;
			case MODE_CROP:// 剪切图片保存
				mCropFragment.backToMain();
				return true;
			case MODE_ROTATE:// 旋转图片保存
				mRotateFragment.backToMain();
				return true;
			}// end switch

			forceReturnBack();
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 强制推出
	 */
	private void forceReturnBack() {
		setResult(RESULT_CANCELED);
		this.finish();
	}

	/**
	 * 保存按钮点击
	 * 
	 * @author panyi
	 */
	private final class ApplyBtnClick implements OnClickListener {
		@Override
		public void onClick(View v) {
			switch (mode) {
			// case MODE_STICKERS:
			// mStirckerFragment.saveStickers();// 保存贴图
			// break;
			// case MODE_FILTER:// 滤镜编辑状态
			// mFliterListFragment.saveFilterImage();// 保存滤镜贴图
			// break;
			case MODE_CROP:// 剪切图片保存
				mCropFragment.saveCropImage();
				break;
			case MODE_ROTATE:// 旋转图片保存
				mRotateFragment.saveRotateImage();
				break;
			case MODE_TEXT:// 文字贴图 图片保存
				mAddTextFragment.saveTextImage();
				break;
			default:
				break;
			}// end switch
		}
	}// end inner class

	/**
	 * 保存按钮 点击退出
	 * 
	 * @author panyi
	 */
	private final class SaveBtnClick implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (!TextUtils.isEmpty(saveFilePath)
					&& new File(saveFilePath).exists()) {

				new AsyncTask<Void, Void, Boolean>() {
					private Dialog dialog;

					@Override
					protected Boolean doInBackground(Void... params) {
						boolean isfinish = proImage(saveFilePath, 1024, 1024);
						return isfinish;
					}

					@Override
					protected void onPreExecute() {
						super.onPreExecute();
						dialog = BaseActivity.getLoadingDialog(
								EditImageActivity.this, "保存中...", false);
						dialog.show();
					}

					@Override
					protected void onPostExecute(Boolean result) {
						super.onPostExecute(result);
						dialog.dismiss();
						if (result) {
							CallbackEventBiz.getInstance()
									.example(saveFilePath);
							// finish();
						}
					}
				}.execute();
			}
			// Intent returnIntent = new Intent();
			// returnIntent.putExtra("save_file_path", saveFilePath);
			// mContext.setResult(RESULT_OK, returnIntent);
			// mContext.finish();
		}
	}// end inner class

	private boolean proImage(String srcPath, float pixWidth, float pixHeight) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;// 只读边,不读内容
		Bitmap bitmap = BitmapFactory.decodeFile(srcPath, options);

		options.inJustDecodeBounds = false;
		int w = options.outWidth;
		int h = options.outHeight;
		int scale = 1;
		if (w > h && w > pixWidth) {
			scale = (int) (options.outWidth / pixWidth);
		} else if (w < h && h > pixHeight) {
			scale = (int) (options.outHeight / pixHeight);
		}
		if (scale <= 0)
			scale = 1;
		options.inSampleSize = scale;// 设置采样率
		options.inPreferredConfig = Config.ARGB_8888;// 该模式是默认的,可不设
		bitmap = BitmapFactory.decodeFile(srcPath, options);
		bitmap = PhotoProcessing.filterPhoto(bitmap, 1);

		File f = new File(srcPath);
		if (f.exists()) {
			f.delete();
		}
		try {
			FileOutputStream out = new FileOutputStream(f);
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
			out.flush();
			out.close();
			return true;
		} catch (FileNotFoundException e) {
			return false;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 保存Bitmap图片到指定文件
	 * 
	 * @param bm
	 */
	public static boolean saveBitmap(Bitmap bm, String filePath) {
		File f = new File(filePath);
		if (f.exists()) {
			f.delete();
		}
		try {
			FileOutputStream out = new FileOutputStream(f);
			bm.compress(Bitmap.CompressFormat.PNG, 100, out);
			out.flush();
			out.close();
			return true;
		} catch (FileNotFoundException e) {
			return false;
		} catch (IOException e) {
			return false;
		}
	}

	/**
	 * 切换底图Bitmap
	 * 
	 * @param newBit
	 */
	public void changeMainBitmap(Bitmap newBit) {
		if (mainBitmap != null) {
			if (!mainBitmap.isRecycled()) {// 回收
				mainBitmap.recycle();
			}
			mainBitmap = newBit;
		} else {
			mainBitmap = newBit;
		}// end if
		mainImage.setImageBitmap(mainBitmap);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mLoadImageTask != null) {
			mLoadImageTask.cancel(true);
		}
	}

}// end class
